package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.house.entity.ShopHistory;
import com.yjps.house.model.Shop;
import com.yjps.house.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house/shop")
public class ShopController {

    @Autowired
    ShopService shopService;

    /**
     * 用户申请门店
     * @param json {apply:{userId:"",reason:""},shop:{"":""...}}
     * @return {shopId:""}
     */
    @RequestMapping(value = "/apply",method = RequestMethod.POST )
    @ResponseBody
    public String apply(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object = JSON.parseObject(json);
        JSONObject apply = JSON.parseObject(object.getString("apply"));
        int userId = Integer.parseInt(apply.getString("userId"));
        String reason=apply.getString("reason");

        Shop shop = JSON.parseObject(object.getString("shop"), Shop.class);
        int shopId=shopService.applyShop(userId,reason,shop);

        //返回
        Message message;
        if (shopId>0){
            Map<String,Object> map=new HashMap<>();
            map.put("shopId",shopId);
            message=new Message(true,"门店申请操作成功",map);
        }else {
            message=new Message(false,"门店申请操作失败");
        }
        return message.toJson();
    }

    /**
     * 系统管理员获取门店申请列表
     * @param startRow
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/apply/list",method = RequestMethod.POST )
    @ResponseBody
    public String shopApplyList(@RequestParam(value = "startRow") String startRow,
                                @RequestParam(value = "pageSize") String pageSize){
        List<ShopHistory> shopHistories=shopService.shopApplyList(Integer.parseInt(startRow), Integer.parseInt(pageSize));
        Message message;
        if (!shopHistories.isEmpty()){
            message=new Message(true,"查询到一些结果",shopHistories);
        }else {
            message=new Message(true,"什么都没查到");
        }
        return message.toJson();
    }

    /**
     * 系统管理员审核门店申请
     * @param userId
     * @param shopId
     * @param result
     * @return
     */
    @RequestMapping(value = "/apply/verfiy",method = RequestMethod.POST )
    @ResponseBody
    public String verfiy(@RequestParam(value = "userId") int userId,
                         @RequestParam(value = "shopId") int shopId,
                         @RequestParam(value = "result") int result){
        shopService.verifyShop(userId,shopId,result);
        Message message=new Message(true,"");
        return message.toJson();
    }

    /**
     * 门店经理更新门店信息
     * @param json {shop:{"":""...}}
     * @return
     */
    @RequestMapping(value = "/apply/update",method = RequestMethod.POST )
    @ResponseBody
    public String updateShop(@RequestParam(value = "json") String json){
        JSONObject object = JSON.parseObject(json);
        Shop shop = JSON.parseObject(object.getString("shop"), Shop.class);
        boolean flag=shopService.updateShop(shop);
        Message message=new Message(flag,"");
        return message.toJson();
    }
}
